<template>
  <ele-modal
    form
    :width="500"
    :model-value="modelValue"
    :title="title"
    @update:modelValue="updateModelValue"
  >
    <ele-page>
      <ele-card class="step-card">
        <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="垫付金额" prop="dianfu_amount">
            <el-input v-model="form.dianfu_amount" style="width: 130px">
              <template #append>元</template>
            </el-input>
          </el-form-item>

          <el-form-item label="超期利率" prop="dianfu_interest_rate">
            <el-input v-model="form.dianfu_interest_rate" style="width: 130px">
              <template #append>%</template>
            </el-input>
          </el-form-item>

          <el-form-item label="还款人" prop="dianfu_grandfather">
            <el-input v-model="form.dianfu_grandfather" style="width: 130px" />
          </el-form-item>
          <el-form-item label="联系方式" prop="dianfu_grandfather_tel">
            <el-input
              v-model="form.dianfu_grandfather_tel"
              style="width: 130px"
            />
          </el-form-item>

          <el-form-item label="还款日" prop="dianfu_deadline">
            <el-date-picker
              v-model="form.dianfu_deadline"
              type="date"
              style="width: 130px"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
          <el-form-item label="材料" prop="dianfu_images">
            <image-upload :limit="10" v-model="form.dianfu_images" />
          </el-form-item>
          <el-form-item v-if="props.data.status == 0 || props.data.status == 4">
            <el-button type="primary" :loading="loading" @click="save">
              保存
            </el-button>
          </el-form-item>
        </el-form>
      </ele-card>
    </ele-page>
  </ele-modal>
</template>

<script setup>
  import { ref, reactive, watch } from 'vue';
  import { updateTradeDianfu } from '@/api/company';
  import { EleMessage } from 'ele-admin-plus/es';
  import ImageUpload from '@/components/ImageUpload/index.vue';

  import { useFormData } from '@/utils/use-form-data';

  const emit = defineEmits(['done', 'update:modelValue']);
  /** 更新modelValue */
  const updateModelValue = (value) => {
    emit('update:modelValue', value);
  };

  const props = defineProps({
    /** 弹窗是否打开 */
    modelValue: Boolean,
    /** 修改回显的数据 */
    data: Object,
    type: String
  });

  const title = ref('');

  /** 提交状态 */
  const loading = ref(false);

  /** 表单实例 */
  const formRef = ref(null);

  /** 表单数据 */
  const [form, resetFields, assignFields] = useFormData({
    id: 0,
    status: 0,
    dianfu_grandfather: '',
    dianfu_grandfather_tel: '',
    dianfu_amount: '', // 金额
    dianfu_interest_rate: '2', // 利率
    dianfu_deadline: '', // 首期还款日
    dianfu_images: '' // 分期材料
  });

  const rules = reactive({
    dianfu_amount: [
      {
        required: true,
        message: '请填写垫付金额',
        type: 'string',
        trigger: 'blur'
      }
    ],
    dianfu_interest_rate: [
      {
        required: true,
        message: '请填写超期利率',
        type: 'string',
        trigger: 'blur'
      }
    ],
    dianfu_deadline: [
      {
        required: true,
        message: '请选择还款日',
        type: 'string',
        trigger: 'blur'
      }
    ],
    dianfu_images: [
      {
        required: true,
        message: '请上传材料',
        type: 'string',
        trigger: 'blur'
      }
    ]
  });

  /** 提交 */
  const save = () => {
    formRef.value?.validate?.((valid) => {
      if (!valid) {
        return;
      }
      var requestData = Object.assign({}, form);

      updateTradeDianfu(requestData)
        .then(() => {
          loading.value = false;
          EleMessage.success('操作成功');
          updateModelValue(false);
          emit('done');
        })
        .catch((e) => {
          loading.value = false;
          console.log(e);
        });
    });
  };

  watch(
    () => props.modelValue,
    (modelValue) => {
      if (modelValue) {
        title.value = props.data.plate_number + '垫付信息录入';
        let d = props.data;
        form.id = d.id;
        if (d.dianfu_amount > 0) {
          assignFields({
            ...props.data
          });
        }
      } else {
        resetFields();
        formRef.value?.clearValidate?.();
        console.log('关闭');
      }
    }
  );
</script>
